<html>
<head>
	<link rel="stylesheet" href="styles/main.css" type="text/css" />
	<script src="javascript/jquery-1.4.min.js"></script>
	<script src="javascript/job.js"></script>
	<script src="javascript/build.js"></script>
	<script src="javascript/checkin_statuses.js"></script>
	<script src="javascript/submitter.js"></script>
	
	<script>
	
	  	var job_url2 = "http://localhost:8080/job/test/";
	  	var job_url = "http://135.251.238.250:8080/view/TLA3.0/view/TLA3.0-CALLP/job/CALLP_CR_MERGE_TEST/";
	
		var checkinStatuses = new CheckinStatuses();
		var retrievedBuilds = 0;
		var urls = 0;
		var retrievedUrlsLimit = 50;
		
	  	$(document).ready(function(){
	  	  $.ajax({
	  	  	url: job_url + "api/json?jsonp=getJobInfo",
	  	  	dataType: "jsonp",
	  	  	method: "GET"
	  	  });
	  	});
	  	
	  	getJobInfo = function(project_json){
	  		job = new Job(project_json);
	  		urls = job.getBuildUrls();
			
      		for(i=0; i<this.canRetrievedUrls(urls);i++){
				retrieveCheckinStatusFromBuild(urls[i]);
			}
			show();
	  	}
	
		canRetrievedUrls = function(urls){
			if(urls.length > retrievedUrlsLimit){
				return retrievedUrlsLimit;
			}
			return urls.length;
		}
	
		show = function(){
			setTimeout(function(){
				if(retrievedBuilds != this.canRetrievedUrls(urls)){
					show();
				}
				else{
					showCheckinStatus();
				}
			}, 10);
		}
		
		retrieveCheckinStatusFromBuild = function(build_url){
			$.ajax({
			    url: build_url + "api/json?jsonp=getBuildInfo",
				cache:false,
				dataType:'jsonp'
			});
		}
	
		getBuildInfo = function(build_json){
			build = new Build(build_json);
			checkinStatuses.addCheckin(build.getDate(), build.getSubmitter());
			retrievedBuilds ++;
		}
		
		showCheckinStatus = function(){
			for(var date in checkinStatuses.getCheckins()){
				submitters = checkinStatuses.getCheckins()[date]
				$(".checkins-box").append(getCheckinStatusForDisplay(date, submitters));
			}
		}
		
		getCheckinStatusForDisplay = function(date, submitters){
			year = date.split("-")[0];
			month = date.split("-")[1];
			day = date.split("-")[2];
			
			var checkinStatusesForDisplay = 
				"<div class='checkin'>" 
				+ "<div class= 'date'> <span class='month'> " + month 
				+ "</span><span class='day'> "+ day + "</span><span class='year'>" + year 
				+"</span><div class='arrow'></div></div>" + "<div class='committer'><ul>" 
				+ getSubmittersForDisplay(submitters)
				+ "</ul></div></div>";
			return checkinStatusesForDisplay;
		}
		
		getSubmittersForDisplay = function(submitters){
			var submitterForDisplay = "";
			for(var i = 0; i<submitters.length; i++){
				submitterForDisplay += "<li><span class='name'>" + submitters[i].getName() + "</span><span class='times'>"+ submitters[i].getCheckinCount()+"</span></li>"
			}
			return submitterForDisplay;
		}
	</script>
</head>
<body>
	<div class="cover"></div>
	<div class="main">
		<div class="subject">Checkin Status in Recent Days: </div>
		<div class="checkins-box"></div>
	</div>
</body>
</html>
